package com.laulkar.activities;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.laulkar.Constants;
import com.laulkar.R;
import com.laulkar.dataproviders.DataProvider;
import com.laulkar.dataproviders.MtaStatus;
import com.laulkar.dataproviders.Subway;
import com.laulkar.dataproviders.SubwayDbAdapter;


public class SubwayList extends ListActivity {

	private SubwayAdapter m_adapter;
	private List<Subway> subway_status;

	public static final String SUBWAY_STATUS_KEY = "key";

	/* Constants for Menu */
	private static final int ADDALARM = 0;
	private static final int HELP = 1;
	private static final int ABOUT = 2;
	private static final int LISTALARM = 3;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//Bundle extras = getIntent().getExtras();
		//subway_status = extras.getParcelableArrayList(SUBWAY_STATUS_KEY);
		//setDisplay();
		//this.subway_status = Main.mta_status.subways;
		SubwayDbAdapter mDbHelper = new SubwayDbAdapter(this);
		mDbHelper.open();
		subway_status = mDbHelper.fetchAllSubway();
		this.m_adapter = new SubwayAdapter(this, R.layout.main, subway_status);
		setListAdapter(this.m_adapter);
	}
	public void setDisplay()
	{
		MtaStatus status = null;
		try {
			status = DataProvider.parseStatus(getResources());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		subway_status = status.subways;

	}

	protected void onListItemClick(ListView l, View v, int position, long id) 
	{
		super.onListItemClick(l, v, position, id);
		Subway s;

		s = (Subway) getListView().getItemAtPosition(position);

		showSubwayStatus(s);

	}

	/* The description of the fault is HTML */
	public void showSubwayStatus(Subway s)
	{
		Intent i = new Intent(this,SubwayDisplayStatus.class);
		i.putExtra(SubwayDisplayStatus.HTML_KEY, s.getText());
		startActivityForResult(i, 4);
	}


	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuItem item;

		item = menu.add(0, ADDALARM,0, R.string.add_alarm);
		item.setIcon(android.R.drawable.ic_menu_add);
		item = menu.add(0, LISTALARM,0, R.string.list_alarm);
		item.setIcon(android.R.drawable.ic_menu_manage);


		item = menu.add(0, HELP,0, R.string.help);
		item.setIcon(android.R.drawable.ic_menu_help);

		item = menu.add(0, ABOUT,0, R.string.about);
		item.setIcon(android.R.drawable.ic_menu_info_details);

		return true;
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {

		case ADDALARM:
			createAlarm();
			break;

		case HELP:
			displayHelp();
			break;

		case ABOUT:
			displayAbout();
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void displayAbout() {
		new AlertDialog.Builder(this).setMessage(R.string.about_text).
		setPositiveButton(android.R.string.ok, null).show();
	}

	private void displayHelp() {

		new AlertDialog.Builder(this).setMessage(R.string.help_text)
		.setPositiveButton(android.R.string.ok, null).
		show();

	}

	private void createAlarm() {
		Intent i = new Intent(this, TimeSelector.class);
		startActivity(i);

	}


	//	/* Display adapters go here */
	private class SubwayAdapter extends ArrayAdapter<Subway> {

		private List<Subway> items;

		public SubwayAdapter(Context context, int textViewResourceId, List<Subway> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.line_row, null);
			}
			Subway s = items.get(position);
			if (s != null) {
				//TextView status = (TextView) v.findViewById(R.id.line);
				ImageView img_status = (ImageView) v.findViewById(R.id.img_status);
				ImageView icon = (ImageView) v.findViewById(R.id.img);
				if(icon != null)
				{
					icon.setImageResource(Constants.subway_icons.get(s.line));


				}
				/*
				if (status != null) {
					status.setText(s.getStatus()); 
					if(s.getStatus().equalsIgnoreCase(Constants.GOOD_SERVICE))
					{
						status.setTextColor(Color.GREEN);
					}
				}
				 */
				if (img_status != null) {

					if(s.getStatus().equalsIgnoreCase(Constants.GOOD_SERVICE))
					{
						img_status.setImageResource(R.drawable.green_led);
					}
					else
						img_status.setImageResource(R.drawable.red_led);
				}
				//if(bt != null){
				//      bt.setText("Status: "+ o.getOrderStatus());
				//}
			}
			return v;
		}
	}
}
